package com.firebird.app.kotlin.stock.trend.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import org.jetbrains.anko.AnkoLogger
import androidx.fragment.app.Fragment
import com.firebird.app.kotlin.stock.trend.event.BaseEvent
import com.firebird.app.kotlin.stock.trend.model.Base
import com.firebird.app.kotlin.stock.trend.utils.Bus

/**
 * Created by ljk on 19/3/9.
 */
abstract class BaseFragment : Fragment(), AnkoLogger {

    abstract val layoutResourceId: Int

    private val STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState != null) {
            val isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN)
            val ft = fragmentManager!!.beginTransaction()
            if (isSupportHidden) {
                ft.hide(this)
            } else {
                ft.show(this)
            }
            ft.commit()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(layoutResourceId, container, false)
    }

    override fun onResume() {
        super.onResume()
        Bus.register(this)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState!!.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden)

    }

    override fun onPause() {
        super.onPause()
        Bus.unregister(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    open fun onEvent(Event: BaseEvent<Base>) {

    }


}